/**
 * Title: PhoneNumberDetailActivity.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2014-1-13 下午2:49:46
 * Version 1.0
 */
package com.luoxudong.phonenumber.ui.activities;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luoxudong.phonenumber.R;
import com.luoxudong.phonenumber.logic.db.model.PhoneNumber;
import com.luoxudong.phonenumber.logic.http.model.response.GetProductDetailRsp;
import com.luoxudong.phonenumber.logic.number.PhoneNumberFactory;
import com.luoxudong.phonenumber.logic.number.interfaces.IGetProductDetailListener;
import com.luoxudong.phonenumber.logic.number.interfaces.IPhoneNumbserManager;
import com.luoxudong.phonenumber.ui.common.listener.OnClickAvoidForceListener;
import com.luoxudong.phonenumber.ui.utils.ToastUtil;
import com.luoxudong.phonenumber.ui.widgets.titlebar.ITitleBarOnClickListener;
import com.luoxudong.phonenumber.ui.widgets.titlebar.TitleBarView;

/** 
 * ClassName: PhoneNumberDetailActivity
 * Description:TODO(这里用一句话描述这个类的作用)
 * Create by 罗旭东
 * Date 2014-1-13 下午2:49:46
 */
public class PhoneNumberDetailActivity extends BaseActivity{
	public static final int MSG_SHOW_TIP = 1;
	private IPhoneNumbserManager phoneNumbserManager = PhoneNumberFactory.getPhoneNumbserManager();
	private TitleBarView titleBarView = null;
	private PhoneNumber phoneNumber = null;
	private GetProductDetailRsp PhoneNumberDetail = null;
	private ImageView logoBrandsImageView = null;
	private TextView logoBrandsTextView = null;
	private TextView numberTextView = null;
	private TextView taocanTextView = null;
	private TextView areaTextView = null;
	private TextView callPriceTextView = null;
	private TextView payTypeTextView = null;
	private TextView merchantNameTextView = null;
	private TextView merchantPhoneTextView = null;
	private TextView merchantAddrTextView = null;
	private RelativeLayout orderFavLayout = null;
	private RelativeLayout orderPhoneLayout = null;
	private TextView orderTotalPriceTextView = null;
	private Button orderBtn = null;
	private boolean isRsponsed = false;
	
	@Override
	protected int getContentViewLayoutResId() {
		return R.layout.pn_detail_layout;
	}

	@Override
	protected void onCreateFindView(Bundle savedInstanceState) {
		titleBarView = (TitleBarView)findViewById(R.id.title_bar);
		logoBrandsImageView = (ImageView)findViewById(R.id.iv_logo_brands);
		logoBrandsTextView = (TextView)findViewById(R.id.tv_brands);
		numberTextView = (TextView)findViewById(R.id.tv_pn_number);
		taocanTextView = (TextView)findViewById(R.id.tv_taocan);
		areaTextView = (TextView)findViewById(R.id.tv_area);
		callPriceTextView = (TextView)findViewById(R.id.tv_call_price);
		payTypeTextView = (TextView)findViewById(R.id.tv_pay_type);
		merchantNameTextView = (TextView)findViewById(R.id.tv_merchant_name);
		merchantPhoneTextView = (TextView)findViewById(R.id.tv_merchant_phone);
		merchantAddrTextView = (TextView)findViewById(R.id.tv_merchant_addr);
		orderFavLayout = (RelativeLayout)findViewById(R.id.rl_order_fav);
		orderPhoneLayout = (RelativeLayout)findViewById(R.id.rl_order_phone);
		orderTotalPriceTextView = (TextView)findViewById(R.id.tv_order_total_price);
		orderBtn = (Button)findViewById(R.id.btn_order);
		
		titleBarView.setTitle(R.string.pn_detail);
		titleBarView.changeMode(TitleBarView.MODEL_BACK);
		setHideProgressDialog(false);
	}

	@Override
	protected void onCreateAddListener(Bundle savedInstanceState) {
		titleBarView.setTitleBarOnClickListener(titleBarOnClickListener);
		orderFavLayout.setOnClickListener(onClickListener);
		orderPhoneLayout.setOnClickListener(onClickListener);
		orderBtn.setOnClickListener(onClickListener);
	}

	@Override
	protected void onCreateInitData(Bundle savedInstanceState) {
		phoneNumber = (PhoneNumber)getIntent().getSerializableExtra("phoneNumber");
		if (phoneNumber == null)
		{
			finish();
			return;
		}
	}

	@Override
	protected void onCreateTaskLoadData() {
		isRsponsed = false;
		phoneNumbserManager.getProductDetailFromService(this, phoneNumber.getPhoneNumber(), new IGetProductDetailListener() {
			
			@Override
			public void onCallbackSuc(GetProductDetailRsp rsp) {
				isRsponsed = true;
			}
			
			@Override
			public void onCallbackFail(int errorCode, String errorMsg) {
				isRsponsed = true;
			}
		});
		
		while (isRsponsed) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
			}
		}
	}

	@Override
	protected void onCreateTaskAddView() {
		logoBrandsImageView.setImageResource(R.drawable.brands_yd);
		logoBrandsTextView.setText("中国移动手机");
		numberTextView.setText(getNumberFormat(phoneNumber.getPhoneNumber()));
		taocanTextView.setText("神州行轻松卡");
		areaTextView.setText("广东省深圳市");
		callPriceTextView.setText("100");
		payTypeTextView.setText("货到付款");
		merchantNameTextView.setText("深圳中号网");
		merchantPhoneTextView.setText("10086");
		merchantAddrTextView.setText("深圳市罗湖区罗芳村");
		orderTotalPriceTextView.setText("200.00");
	}

	@Override
	protected void refreshUI(Object obj) {
		
	}
	
	@Override
	protected void handleMessage(Message msg) {
		switch (msg.what) {
		case MSG_SHOW_TIP:
			ToastUtil.showToast(PhoneNumberDetailActivity.this, (String)msg.obj);
			break;

		default:
			break;
		}
	}
	
	private void goback()
	{
		isRsponsed = true;
	}
	
	/**
	 * 自动下拉刷新界面
	 * 
	 */
	public void onRefreshing()
	{
		
	}
	
	private Spanned getNumberFormat(String phoneNumber)
	{
		Spanned htmlString = null;
		if (phoneNumber == null || phoneNumber.length() < 11)
		{
			return Html.fromHtml(phoneNumber);
		}
		
		htmlString = Html.fromHtml("<font color=\"#EF8F65\">" + phoneNumber.substring(0,3) + "</font> <font color=\"#007CC3\">" + phoneNumber.substring(3,7) + "</font> <font color=\"#DA251D\">" + phoneNumber.substring(7,11) + "</font>");
		return htmlString;
	}
	
	private void doOrderFav()
	{
		sendMessage(obtainMessage(MSG_SHOW_TIP, "收藏成功!"));
	}
	
	private void doOrderPhone()
	{
		Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:10086"));
        startActivity(intent);
	}
	
	private void order()
	{
		Intent intent = new Intent(this, OrderActivity.class);
		intent.putExtra("phoneNumber", phoneNumber);
		startActivity(intent);
	}
	
	private OnClickAvoidForceListener onClickListener = new OnClickAvoidForceListener()
	{

		@Override
		public void onClickAvoidForce(View v) {
			switch (v.getId()) {
			case R.id.rl_order_fav:
				doOrderFav();
				break;
			case R.id.rl_order_phone:
				doOrderPhone();
				break;
			case R.id.btn_order:
				order();
				break;
			default:
				break;
			}
		}
		
	};
	
	private ITitleBarOnClickListener titleBarOnClickListener = new ITitleBarOnClickListener() {
		
		@Override
		public void onRightClick(View v) {
		}
		
		@Override
		public void onLeftClick(View v) {
			goback();
			finish();
		}
	};
}
